<?php

$db_obj = new db_obj();
$client_obj = new client_obj();
$user_obj = new user_obj();
$filter_obj = new filter_obj();

if (isset($_REQUEST['action'])) {
    switch ($_REQUEST['action']) {
        case 'deleteUsers':
            if (!empty($_REQUEST['userId'])) {
                $success = true;
                foreach ($_REQUEST['userId'] as $key => $id) {
                    $res = $user_obj->deleteUserById($id);
                    if (error_obj::isError($res)) {
                        error_obj::setError($res, UNEXCEPTED);
                        $success = false;
                    }
                }
                if ($success) {
                    error_obj::setGood("Users were deleted!");
                    base_obj::redirect(ROOT . '?input=list_of_users');
                }
            }
            break;
    }
}

if (!isset($_SESSION['list_of_users_pager'])) {
    $_SESSION['list_of_users_pager'] = $GLOBALS['list_of_users_pager'];
}

if (!isset($_SESSION['list_of_users_order'])) {
    $_SESSION['list_of_users_order'] = $GLOBALS['list_of_users_order'];
}

if (isset($_SESSION['list_of_users_fulltext']) && !empty($_SESSION['list_of_users_fulltext']))
    $GLOBALS['template_data']['currentFulltext'] = $_SESSION['list_of_users_fulltext'];

$filter = array();

$currentFilter = array();

if (isset($_SESSION['list_of_users_filter'])) {
    $filter = $_SESSION['list_of_users_filter'];
    $result = $filter_obj->updateFilter($_SESSION['user']['user_id'], 'list_of_users_filter', $filter);
    if (error_obj::isError($result)) {
        error_obj::setError($result, UNEXCEPTED);
    }
    foreach ($_SESSION['list_of_users_filter'] as $f => $v) {
        switch ($f) {
            case 'userlevel':
                $currentFilter[$f] = $GLOBALS['userLevels'][$v];
                break;
            case 'client':
                $currentFilter[$f] = $client_obj->getClient($v, array('name'));
                $currentFilter[$f] = $currentFilter[$f][0];
                break;
            case 'active':
                if ($v == 0)
                    $currentFilter[$f] = 'No';
                else
                    $currentFilter[$f] = 'Yes';
                break;
            case 'prefered':
                if ($v == 0)
                    $currentFilter[$f] = 'No';
                else
                    $currentFilter[$f] = 'Yes';
                break;
        }
    }
}

$GLOBALS['template_data']['currentFilter'] = $currentFilter;

if (!isset($_GET['page'])) {
    $_GET['page'] = 0;
}

$limit = ($_GET['page'] * $_SESSION['list_of_users_pager']) . ' ,' . $_SESSION['list_of_users_pager'];

if ($_SESSION['user']['profile']['userlevel'] == USER_TYPE_CLIENT) {
    $filter['clients'] = $_SESSION['user']['clients'];
}
if (isset($_SESSION['list_of_users_fulltext']) && !empty($_SESSION['list_of_users_fulltext'])) {
    $PAGER_COUNT = $user_obj->getUsersByFulltext($_SESSION['list_of_users_fulltext'], $filter, $_SESSION['list_of_users_order'], '', 'COUNT(*)');
    $users = $user_obj->getUsersByFulltext($_SESSION['list_of_users_fulltext'], $filter, $_SESSION['list_of_users_order'], $limit);
} else {
    $PAGER_COUNT = $user_obj->getUsers('COUNT(*)', $filter, $_SESSION['list_of_users_order']);
    $users = $user_obj->getUsers('*', $filter, $_SESSION['list_of_users_order'], $limit);
}
$PAGER_COUNT = $PAGER_COUNT[0][0];
$GLOBALS['template_data']['total_count'] = $PAGER_COUNT;
$GLOBALS['template_data']['currentOrder'] = array(
    $_SESSION['list_of_users_order']['col'],
    $_SESSION['list_of_users_order']['order']
);

foreach ($users as $key => $user) {
    $users[$key]['userlevel_info'] = $GLOBALS['userLevels'][$user['userlevel']];
    $users[$key]['client_info'] = $client_obj->getClient($user['client'], array('name'));
}


$GLOBALS['template_data']['users'] = $users;

$pager = $db_obj->pager($PAGER_COUNT, $_GET['page'], $_SESSION['list_of_users_pager']);

$GLOBALS['template_data']['pager'] = $pager;
?>
